using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DuplicateFileFinder
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private EventManager mEventManger = null;
        private Engine mEngine = null;
        private frmDataMatches mDataMatches = null;
        private frmNameMatches mNameMatches = null;
        private frmResultList mResultList = null;
        private frmSearchPaths mSearchPaths = null;

        private void frmMain_Load(object sender, EventArgs e)
        {
            mEventManger = new EventManager();
            mEventManger.EventUpdateProgress +=new EventManager.DelegateUpdateProgress(UpdateProgress);
            mEventManger.EventUpdateViewer +=new EventManager.DelegateUpdateViewer(UpdateViewer);

            
            mResultList = new frmResultList();
            mResultList.MdiParent = this;
            mResultList.Show(dpnlMain);
            mResultList.DockState = WeifenLuo.WinFormsUI.Docking.DockState.DockLeft;

            mSearchPaths = new frmSearchPaths();
            mSearchPaths.MdiParent = this;
            mSearchPaths.Show(dpnlMain);
            mSearchPaths.DockState = WeifenLuo.WinFormsUI.Docking.DockState.DockBottom;

            mDataMatches = new frmDataMatches();
            mDataMatches.MdiParent = this;
            mDataMatches.Show(dpnlMain);
            mDataMatches.DockState = WeifenLuo.WinFormsUI.Docking.DockState.Document;

            mNameMatches = new frmNameMatches();
            mNameMatches.MdiParent = this;
            mNameMatches.Show(dpnlMain);
            mNameMatches.DockState = WeifenLuo.WinFormsUI.Docking.DockState.Document;

            mEngine = new Engine(mEventManger);

            mEventManger.EventUpdateViewer += new EventManager.DelegateUpdateViewer(mSearchPaths.UpdateViewer);
            mEventManger.EventUpdateViewer += new EventManager.DelegateUpdateViewer(mDataMatches.UpdateViewer);
            mEventManger.EventUpdateViewer += new EventManager.DelegateUpdateViewer(mNameMatches.UpdateViewer);
            mEventManger.EventUpdateViewer += new EventManager.DelegateUpdateViewer(mResultList.UpdateViewer);

            mSearchPaths.InitViewer(mEngine);
            mDataMatches.InitViewer(mEngine);
            mNameMatches.InitViewer(mEngine);
            mResultList.InitViewer(mEngine);
 
        }


        private void UpdateProgress(int pTotal, int pCurrent, string pDescription)
        {
            if (InvokeRequired)
            {
                this.Invoke(new EventManager.DelegateUpdateProgress(UpdateProgress), new object[] { pTotal, pCurrent, pDescription });
            }
            else
            {
                if (pbCurrentProgress.Maximum != pTotal)
                {
                    pbCurrentProgress.Maximum = pTotal;
                }
                if (pTotal != 0)
                {
                    pbCurrentProgress.Value = pCurrent % pTotal;
                }

                pbCurrentProgress.ToolTipText = pCurrent.ToString() + " out of " + pTotal.ToString();
                txtStatusDetails.Text = "Total " + pDescription + " " + pCurrent.ToString() + " out of " + pTotal.ToString();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {


            //mEngine.SaveResultXml();
            mEngine.StartCompare();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            mEngine.StopCompare();
        }

        private void UpdateViewer()
        {
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            mEngine.Save();
            MessageBox.Show("Saved!");
        }
        
    }
}